###############################################
# Meta-analysis #7: Social group participation
###############################################

# 0) User paths  edit these
PATH <- ""

path_meta_group_participation        <- file.path(PATH, "Datasets", "meta.group_participation.csv")
path_meta_group_participation_biv    <- file.path(PATH, "Datasets", "meta.group_participation_biv.csv")
path_meta_group_participation_random <- file.path(PATH, "Datasets", "meta.group_participation_random.csv")
path_out_csv                         <- file.path(PATH, "Results",  "7.results_group_participation_models.csv")

# 1) Settings
outcome_name <- "Social group participation"

# 2) Packages
suppressPackageStartupMessages({
  library(metafor)
})

# 3) Load data
meta.group_participation     <- read.csv(path_meta_group_participation,     stringsAsFactors = FALSE)
meta.group_participation_biv <- read.csv(path_meta_group_participation_biv, stringsAsFactors = FALSE)
meta.group_participation_random <- read.csv(path_meta_group_participation_random, stringsAsFactors = FALSE)

# 4) Basic checks
required_cols <- c("authoryear", "yi", "vi")
if (!all(required_cols %in% names(meta.group_participation))) {
  stop("meta.group_participation is missing required columns: ",
       paste(setdiff(required_cols, names(meta.group_participation)), collapse = ", "))
}
if (!all(required_cols %in% names(meta.group_participation_biv))) {
  stop("meta.group_participation_biv is missing required columns: ",
       paste(setdiff(required_cols, names(meta.group_participation_biv)), collapse = ", "))
}
if (!all(required_cols %in% names(meta.group_participation_random))) {
  stop("meta.group_participation_random is missing required columns: ",
       paste(setdiff(required_cols, names(meta.group_participation_random)), collapse = ", "))
}

# 5) Full, all  three level RE
full_all_fit <- rma.mv(
  yi, vi,
  random = ~ 1 | authoryear/row.names(meta.group_participation),
  data   = meta.groups,
  test   = "t"
)
full_all_df <- data.frame(
  coef        = unname(full_all_fit$beta),
  ci.lb95     = full_all_fit$ci.lb,
  ci.ub95     = full_all_fit$ci.ub,
  pval        = full_all_fit$pval,
  outcome     = outcome_name,
  n           = length(unique(meta.group_participation$authoryear)),
  k           = length(meta.group_participation$authoryear),
  sample_type = "All",
  model_type  = "Full",
  stringsAsFactors = FALSE
)

# 6) Bivariate, all  three level RE
biv_all_fit <- rma.mv(
  yi, vi,
  random = ~ 1 | authoryear/row.names(meta.group_participation_biv),
  data   = meta.groups_biv,
  test   = "t"
)
biv_all_df <- data.frame(
  coef        = unname(biv_all_fit$beta),
  ci.lb95     = biv_all_fit$ci.lb,
  ci.ub95     = biv_all_fit$ci.ub,
  pval        = biv_all_fit$pval,
  outcome     = outcome_name,
  n           = length(unique(meta.group_participation_biv$authoryear)),
  k           = length(meta.group_participation_biv$authoryear),
  sample_type = "All",
  model_type  = "Bivariate",
  stringsAsFactors = FALSE
)

# 7) Full, random  build subset inside the script
full_random_fit <- rma.mv(
  yi, vi,
  random = ~ 1 | authoryear/row.names(meta.group_participation_random),
  data   = meta.group_participation_random,
  test   = "t"
)
full_random_df <- data.frame(
  coef        = unname(full_random_fit$beta),
  ci.lb95     = full_random_fit$ci.lb,
  ci.ub95     = full_random_fit$ci.ub,
  pval        = full_random_fit$pval,
  outcome     = outcome_name,
  n           = length(unique(meta.group_participation_random$authoryear)),
  k           = length(meta.group_participation_random$authoryear),
  sample_type = "Quasi-experimental",
  model_type  = "Full",
  stringsAsFactors = FALSE
)

# 8) Combine and save
out_models <- rbind(full_all_df, biv_all_df, full_random_df)

print(out_models, row.names = FALSE)

if (!is.null(path_out_csv)) {
  write.csv(out_models, path_out_csv, row.names = FALSE)
}
